<?php
namespace app\index\controller;
use app\index\model\Login_model;
use think\Controller;
use think\Cookie;
use think\Db;
use think\Request;
use think\Session;

class Login extends Controller {

	// 登录
	public function index() {
		//    $Test = new \my\Test();
		// $Test->check();

		return view('index@Login/index');
	}
	// 处理登录逻辑
	public function doLogin($username = '', $password = '') {
		// session_start();
		//接收用户名和密码
		$rev = request()->post();

		$username = $rev['username'];
		$password = $rev['password'];

		// $uid = $_SESSION['think']['username'][0]['user_id'];
		//验证
		// $user = M('a_user');
		$data = Db::name('_user')->where('user_name', $username)->where('is_delete', 0)->select();
		if (!$data) {
			// $this->error('用户名不存在！');
			// exit;
			$info = '用户名不存在';
			return $info;
		}
		//验证密码
		if ($data[0]['user_pass'] != md5($password)) {
			//  $this->error('密码不正确');
			// exit;
			$info = '密码不正确';
			return $info;
		}

		Session::set('username', $data);
		if (isset($rev['remember']) && $rev['remember'] == '1') {
			Cookie::set('username', $username, 604800);
			Cookie::set('password', $password, 604800);
		}
		$code = Db::name('_user')->field('user_id')->where('user_name', $username)->find();
		$id = $code['user_id'];
		//是否是领导
		$level = Db::table('cn_user')
			->where('user_id', $id)
			->field('level')
			->find();
		$_SESSION['think']['username']['level'] = $level['level'];
		// $list = Db::table('cn_user')
		//         ->alias('cu')
		//         ->join('cn_user_character_relation cucr', 'cucr.user_id  = cu.user_id')
		//         ->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
		//         ->join('cn_priv_character_relation cpcr', 'cpcr.chara_id  = cc.chara_id')
		//         ->join('cn_priv cp', 'cp.priv_id = cpcr.priv_id')
		//         ->where('cu.user_id',$id)
		//         ->field('cp.mname,cp.aname')
		//         ->select();
		$k = [];
		if ($id == '1') {

		} else {

			$list = Db::table('cn_user')
				->alias('cu')
				->join('cn_user_character_relation cucr', 'cucr.user_id  = cu.user_id')
			// ->join('cn_character cc', 'cc.chara_id = cucr.chara_id')
			// ->join('cn_priv_character_relation_new cpcr', 'cpcr.chara_id  = cc.chara_id')
			// ->join('cn_priv_new cp', 'cp.priv_id = cpcr.priv_id')
				->where('cu.user_id', $id)
				->field('chara_id')
			// ->field('cp.rule')
				->find();
			//根据chara_id查rule
			$point = explode(',', $list['chara_id']);
			$p = '';
			for ($i = 0; $i < count($point); $i++) {
				$aaa = '';
				$cn[$i] = '';
				$corule[$i] = Db::table('cn_character')
					->alias('cc')
					->join('cn_priv_character_relation_new cpcr', 'cpcr.chara_id  = cc.chara_id')
					->join('cn_priv_new cp', 'cp.priv_id = cpcr.priv_id')
					->where('cc.chara_id', $point[$i])
					->field('cp.rule')
					->select();
				for ($j = 0; $j < count($corule[$i]); $j++) {
					$cn[$i] .= $corule[$i][$j]['rule'];
					for ($q = 0; $q < count($cn); $q++) {
						$aaa .= $cn[$q];
						$p = rtrim($aaa, ',');
					}
				}
			}
			if (empty($p)) {
				$k = [];
			} else {
				$k = explode(",", $p);
			}

		}

		//         $p = '';
		// foreach ($list as $key => $value) {

		//      $p .= $value['rule'];
		// }
		// $p = rtrim($p, ',');
		// $sql = "SELECT
		//             cp.rule
		//         FROM
		//             cn_user AS cu
		//             LEFT JOIN cn_user_character_relation AS cucr ON cucr.user_id  = cu.user_id
		//             LEFT JOIN cn_character AS cc ON cc.chara_id = cucr.chara_id
		//             LEFT JOIN cn_priv_character_relation_new AS cpcr ON cpcr.chara_id  = cc.chara_id
		//             LEFT JOIN cn_priv_new AS cp ON cp.priv_id = cpcr.priv_id

		//         WHERE
		//             cu.user_id = ".$id."
		//         ";
		//         dump($sql);die;
		// $list = Db::query($sql);
		//控制器名转换为大写
		// foreach ($list as $key => $val) {
		//     $list[$key]['mname'] = ucfirst($val['mname']);
		// }
		//查看查询出来的信息
		// V($list); exit;
		// $nodelist = array();
		// $nodelist['Index'] = array('index');
		// //遍历重新拼装
		// foreach($list as $v){
		//     $nodelist[$v['mname']][] = $v['aname'];
		//     //把修改和执行修改 添加和执行添加 拼装到一起
		//     if($v['aname']=="edit"){
		//         $nodelist[$v['mname']][]="update";
		//     }
		//     if($v['aname']=="add"){
		//         $nodelist[$v['mname']][]="insert";
		//     }
		// }

		//将权限信息放置到session中
		// $_SESSION['think']['username']['nodelist'] = $nodelist;
		// $_SESSION['think']['username']['nodelist'] = $p;
		Session::set('nodelist', $k);
		// var_dump($id);die;
		$rev = new Login_model;
		$res = $rev->getindex($id);
		// $a = $_SESSION['think']['username']['user_id'];
		// $b = $_SESSION['think']['username']['user_name'];
		// var_dump($res);die;

		return view('index@Index/index', [
			'node' => $res['node'],
			'amount' => $res['amount'],
			'mail' => $res['mail'],
			'id' => $res['id'],
			'file_num' => $res['file_num'],
			'borrow_num' => $res['borrow_num'],
			'user_num' => $res['user_num'],
		]);

		// return $id;
		// //验证
		// // $user = M('a_user');
		// $data = Db::name('_user')->where('user_name',$username)->select();
		// // var_dump($data);die;
		// if (!$data) {
		//     $this->error('用户名不存在！');
		//     exit;
		// }
		// //验证密码
		// if ($data[0]['user_pass'] != md5($password)) {
		//     $this->error('密码不正确');
		//     exit;
		// }
		// // var_dump($data);die;
		// //把用户信息添加到session
		// // $_SESSION['username'] = $data;
		// Session::set('username',$data);

		// // var_dump($_SESSION);die;

		// //根据用户id获取对应的节点信息
		// //$sql = "select n.mname,n.aname from lamp_user u join lamp_user_role ur on u.id=ur.uid join lamp_role_node rn on ur.rid=rn.rid join lamp_node n on rn.nid=n.id where u.id={$users['id']}";
		// //$list = M()->query($sql);

		// $res = Db::name('_user')->field('user_id')->where('user_name',$username)->find();
		// $id = $res['user_id'];
		// // var_dump($id);
		// // $list = Db::table('cn_user')
		// //      ->alias('cu')
		// //      ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
		// //      ->join('cn_character cc','cc.chara_id = cucr.chara_id')
		// //      ->join('cn_priv_character_relation cpcr','cpcr.chara_id = cc.chara_id')
		// //      ->join('cn_priv cp','cp.priv_id = cpcr.priv_id')
		// //      ->where('cn_user.user_id',$id)
		// //         ->field('cp.mname,cp.aname')
		// //         ->select();
		// $list = Db::table('cn_user')
		//         ->alias('cu')
		//         ->join('cn_user_character_relation cucr','cu.user_id = cucr.user_id')
		//         ->join('cn_character cc','cc.chara_id = cucr.chara_id')
		//         ->join('cn_menu_character_relation cmcr','cmcr.chara_id = cc.chara_id')
		//         ->join('cn_menu cm','cmcr.menu_id = cm.menu_id')
		//         ->where('cn_user.user_id',$id)
		//         ->field('menu_name')
		//         ->select();
		// var_dump($list);die;

		// //控制器名转换为大写
		// foreach ($list as $key => $val) {
		//     $list[$key]['mname'] = ucfirst($val['mname']);
		// }
		// //查看查询出来的信息

		// $nodelist = array();
		// $nodelist['Index'] = array('index');
		// // 遍历重新拼装
		// foreach($list as $v){
		//     $nodelist[$v['mname']][] = $v['aname'];
		//     //把修改和执行修改 添加和执行添加 拼装到一起
		// }
		// // var_dump($nodelist);die;
		// //将权限信息放置到session中
		// $_SESSION['think']['username']['nodelist'] = $nodelist;
		// Session::set('username',$nodelist);
		// var_dump($_SESSION['think']['username'][0]['user_name']);die;
		//重组的信息
		// V($_SESSION);exit;
		// var_dump($_SESSION['think']['username']['nodelist']);die;
		//跳转到首页
		// $this->redirect('Index/index');

	}

}
